<template>
    <p>护理项目</p>
    <el-button type="primary" @click="addProject">新增护理</el-button>
    <br><br>
    <!-- 表单 -->
    <el-form :model="projectFormData" inline="true">
        <el-form-item label="请输入名称">
            <el-input v-model="projectFormData.name" />
        </el-form-item>
        <el-form-item label="状态">
            <el-select v-model="projectFormData.status" placeholder="请选择状态" style="width: 200px;">
                <el-option label="1" value="1" />
                <el-option label="0" value="0" />
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="selectSubmit">查询</el-button>
            <el-button type="primary" @click="cleanSubmit">重置</el-button>
        </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="ID" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="unit" label="单位" width="70" />
        <el-table-column prop="price" label="价格" width="70" />
        <el-table-column prop="image" label="图片">
            <template #default="scope">
                <el-image style="width: 50px; height: 50px" :src="scope.row.image" />
            </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="status" label="状态" width="70" />
        <el-table-column prop="nursingRequirement" label="备注" width="230" />
        <el-table-column label="操作">
            <template #default="scope">
                <el-button size="small" @click="handleEdit(scope.row.id)">
                    编辑
                </el-button>
                <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">
                    删除
                </el-button>
            </template>
        </el-table-column>
    </el-table>
    <br>
    <!-- 分页 -->
    <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[5, 10, 15, 20]"
        background="true" layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    <!-- 对话框 -->
    <el-dialog v-model="dialogTableVisible" :title="title" width="800" :ref="dialogRef">
        <el-form :model="projectFrom" label-width="80px">
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="名称">
                        <el-input v-model="projectFrom.name" placeholder="请输入名称" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="说明">
                        <el-input v-model="projectFrom.nursingRequirement" placeholder="请备注" />
                    </el-form-item>
                </el-col>

                <el-col :span="12">
                    <el-form-item label="排序号">
                        <el-input v-model="projectFrom.orderNo" placeholder="请输入排序号" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="单价">
                        <el-input v-model="projectFrom.price" placeholder="请输入单价" />
                    </el-form-item>
                </el-col>

                <el-col :span="12">
                    <el-form-item label="状态">
                        <el-select v-model="projectFrom.status" placeholder="请选择">
                            <el-option label="1" value="1" />
                            <el-option label="0" value="0" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="单位">
                        <el-input v-model="projectFrom.unit" placeholder="请输入单位" />
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="submitForm">
                    保存
                </el-button>
                <el-button @click="visible">取消</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
import { pageFindAll, saveProject, deleteProject, updateProject, getProjectById } from '@/api/project'
import { ElMessage } from 'element-plus'
import { onMounted, ref } from 'vue'

const pageNum = ref(1)
const pageSize = ref(5)
const tableData = ref([])

// 删除
const handleDelete = async (id) => {
    let result = await deleteProject(id)
    if (result.code == 200) {
        ElMessage.success('删除成功！')
        selectPage()
    } else {
        ElMessage.error('删除失败！')
    }
}

// 编辑
const handleEdit = async (id) => {
    let result = await getProjectById(id)
    projectFrom.value = result.data
    dialogTableVisible.value = true
    title.value = '编辑护理'
}

// 对话框
const title = ref('')
const dialogTableVisible = ref(false)
const projectFrom = ref({
    name: '',
    nursingRequirement: '',
    orderNo: null,
    price: 0,
    status: 0,
    unit: ''
})
const dialogRef = ref(null)
const submitForm = async () => {
    if (dialogRef.value) {
        dialogRef.value.resetFields()
    }
    let result = null
    if (projectFrom.value.id) {
        result = await updateProject(projectFrom.value)
    } else {
        result = await saveProject(projectFrom.value)
    }
    if (result.code == 200) {
        ElMessage.success('操作成功！')
        dialogTableVisible.value = false
        selectPage()
        projectFrom.value = {
            name: '',
            nursingRequirement: '',
            orderNo: null,
            price: 0,
            status: 0,
            unit: ''
        }
    } else {
        ElMessage.error('操作失败！')
    }
}
const visible = () => {
    dialogTableVisible.value = false
    projectFrom.value = {
        name: '',
        nursingRequirement: '',
        orderNo: null,
        price: 0,
        status: 0,
        unit: ''
    }
}

const addProject = () => {
    dialogTableVisible.value = true
}

const selectPage = async () => {
    let result = await pageFindAll(projectFormData.value.name, pageNum.value, pageSize.value, projectFormData.value.status)
    tableData.value = result.data.rows
    total.value = result.data.total
}

// 分页
const total = ref(0)
const handleSizeChange = (val) => {
    pageSize.value = val
    pageNum.value = 1
    selectPage()
}
const handleCurrentChange = (val) => {
    pageNum.value = val
    selectPage()
}

// 表单
const projectFormData = ref({
    name: '',
    status: ''
})
const selectSubmit = () => {
    selectPage()
}
const cleanSubmit = () => {
    projectFormData.value = {
        name: '',
        status: ''
    }
    selectPage()
}

onMounted(() => {
    selectPage()
})
</script>

<style scoped></style>